package com.action;

import com.opensymphony.xwork2.ModelDriven;
import com.page.Pagination;
import com.page.PaginationImpl;
import com.model.Message;
import com.model.Users;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.dao.*;

public class MessageAction implements ModelDriven<Message>,ServletRequestAware{
	private Message message;
	private Users users;
	private HttpServletRequest request;
	MessageDao messageDao=new MessageDao();
	
	
	
	public Message getMessage() {
		return message;
	}
	public void setMessage(Message message) {
		this.message = message;
	}
	public Users getUsers() {
		return users;
	}
	public void setUsers(Users users) {
		this.users = users;
	}
	
	public String show() {
		HttpSession session=request.getSession();
		Pagination page= new PaginationImpl();
		page.setPageSize(3);
		String function=request.getParameter("function");
		String pageNumber=request.getParameter("pageNumber");
		if(pageNumber==null) page.setPageNumber(1);
		else page.setPageNumber(Integer.parseInt(pageNumber));
		if(function==null) {
			function=(String)session.getAttribute("function");
			if(function==null)function="all";
		}
		session.setAttribute("function",function);
		if("my".equals(function)){
			Users u=(Users)request.getSession().getAttribute("loginUser");
			page = messageDao.getAll(page,u.getUsername());
		}else page = messageDao.getAll(page);
		request.setAttribute("mslist", page);
		return "success";
	}

	public Message getMessage1() {
		return message;
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;
		
	}
	@Override
	public Message getModel() {
		message =new Message();
		return this.getMessage();		
		}
	}
	

